package main

import (
	"fmt"
	"net/http"
)

type MyHandler struct {}

func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)  {
	fmt.Fprintf(w, "Hello World!")
}

type HelloHandler struct {}

func (h *HelloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)  {
	fmt.Fprintf(w, "Hello !")
}

type WorldHandler struct {}

func (h *WorldHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)  {
	fmt.Fprintf(w, "World !")
}

func main() {
	hello := HelloHandler{}
	world := WorldHandler{}

	//使用多个处理器的时候不能在Server的结构体中定义handler，要通过http.Handle函数将处理器绑定至DefaultServeMux
	server := http.Server{
		Addr:              "0.0.0.0:8080",
	}

	//接收者是指针类型，所以要传结构体的指针
	//如果是接收者是值，既可以用结构体的值，也可以用结构体的指针
	http.Handle("/hello", &hello)
	http.Handle("/world", &world)

	server.ListenAndServe()
}
